<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>杂色</title>
  <link rel="icon" href="https://img.kaikeba.com/kkb_portal_icon.ico">
  <style>
    body {
      margin: 0;
      overflow: hidden;
    }

    #canvas {
      background-color: antiquewhite;
    }
  </style>
</head>

<body>
  <canvas id="canvas"></canvas>
  <!-- 顶点着色器 -->
  <script id="vertexShader" type="x-shader/x-vertex">
    attribute vec4 a_Position;
    void main(){
      gl_Position=a_Position;
    }
  </script>
  <!-- 片元着色器 -->
  <script id="fragmentShader" type="x-shader/x-fragment">
    precision mediump float;

    uniform float u_Ang;
    float s=sin(u_Ang);
    float c=cos(u_Ang);
    mat2 m=mat2(
      c, s,
      -s,c
    );
    
    float rand(vec2 fragCoord){
      vec2 a= m*vec2(0.1234,0.5678);
      float n= dot(fragCoord,a);
      return fract(sin(n)*10000.0);
    }

    void main(){
      float f = rand(gl_FragCoord.xy);
      gl_FragColor = vec4(f, f, f, 1);
    }
  </script>
  <script type="module">
    import { initShaders, parseColorStops } from "../jsm/Utils.js";
    import Poly from './jsm/Poly.js'

    const canvas = document.querySelector("#canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    // 获取着色器文本
    const vsSource = document.querySelector("#vertexShader").innerText;
    const fsSource = document.querySelector("#fragmentShader").innerText;

    //三维画笔
    const gl = canvas.getContext("webgl");

    //初始化着色器
    initShaders(gl, vsSource, fsSource);

    //声明颜色 rgba
    gl.clearColor(0, 0, 0, 1);

    const source = new Float32Array([
      -1, 1,
      -1, -1,
      1, 1,
      1, -1
    ]);

    const rect = new Poly({
      gl,
      source,
      type: 'TRIANGLE_STRIP',
      attributes: {
        a_Position: {
          size: 2,
          index: 0
        }
      },
      uniforms: {
        u_Ang: {
          type: 'uniform1f',
          value: 0
        }
      }
    })

    // gl.clear(gl.COLOR_BUFFER_BIT);
    // rect.draw()

    let ang = 1;
    !(function ani() {
      ang++;
      rect.uniforms.u_Ang.value = ang;
      rect.updateUniform()
      gl.clear(gl.COLOR_BUFFER_BIT);
      rect.draw()
      requestAnimationFrame(ani)
    })()

  </script>
</body>

</html>